package com.peng.config;

import com.peng.mapper.UserMapper;
import com.peng.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//自定义用户登录成功之后跳转地址
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    private String url;
    private boolean isRedirect;

    public MyAuthenticationSuccessHandler(String url, boolean isRedirect) {
        this.url = url;
        this.isRedirect = isRedirect;
    }

    @Autowired
    private UserMapper userMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        System.out.println(authentication.getName());
        User users = userMapper.selectUserByUserName(authentication.getName());
        //System.out.println(users);
        String urls = "";
        //switch (user.getRoleId()){
        //    case 1:
        //        urls = "/admin/login";
        //        break;
        //    default:
        //        urls="/index";
        //        break;
        //}
        if (isRedirect) {
            httpServletResponse.sendRedirect(urls);
        } else {
            httpServletRequest.getRequestDispatcher(urls).forward(httpServletRequest, httpServletResponse);
        }
    }
}
